// components/UploadImage/index.js
const baseUrl = require('../../pages/controllers/controllers').baseUrl;

Component({
  /**
   * 组件的属性列表
   */
  properties: {
     isOpen:{
       type:Boolean,
       value:true
     }
  },

  
  /**
   * 组件的初始数据
   */
  data: {
    
  },
  
  /**
   * 组件的方法列表
   */
  methods: {
  open(){
      this.setData({
        isOpen:false
      })
  },
    //从相册中选取照片
  handlePhotos(e){
     let type=e.target.dataset.type
      
     if(!type){
       return
     }
     let that=this
     wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      success (res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        let img=tempFilePaths[0]
     
        that.uploadAvatarImg(img)
      }
    })
     
     
    
  },
   //上传图片
   uploadAvatarImg(e){
    console.log('e==>',e)
     let that=this
    wx.uploadFile({
      url: baseUrl+'/upload/picture',
      filePath: e,
      name: "file",
      success: function (res) {
      console.log('succeess',res)
      let img=JSON.parse(res.data).data.baseData[0]
      console.log('img==>',img)
        that.setData({
          portrait:img,
        })
        
        that.triggerEvent('handlechange',img)
      }
  })
   },
   //取消
   handleCancel(e){
      this.setData({
         isOpen:true
      })
   },

  }
})
